<?php

/*
 * This file is part of the symfony package.
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfWidgetFormTextareaTinyMCE represents a Tiny MCE widget.
 *
 * You must include the Tiny MCE JavaScript file by yourself.
 *
 * @package    symfony
 * @subpackage widget
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id$
 */
class sfWidgetFormTextareaTinyMCE extends sfWidgetFormTextarea
{
  /**
   * Constructor.
   *
   * Available options:
   *
   *  * theme:  The Tiny MCE theme
   *  * width:  Width
   *  * height: Height
   *  * config: The javascript configuration
   *
   * @param array $options     An array of options
   * @param array $attributes  An array of default HTML attributes
   *
   * @see sfWidgetForm
   */
  protected function configure($options = array(), $attributes = array())
  {
    $this->addOption('theme', 'modern');
    $this->addOption('width');
    $this->addOption('height');
    $this->addOption('config', '');
  }

  /**
   * @param  string $name        The element name
   * @param  string $value       The value selected in this widget
   * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
   * @param  array  $errors      An array of errors for the field
   *
   * @return string An HTML tag string
   *
   * @see sfWidgetForm
   */
  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
  	
    $textarea = parent::render($name, $value, $attributes, $errors);
	//mode : "specific_textareas",
	//editor_selector : "mceEditor",
    
    $js = sprintf(<<<EOF
<script type="text/javascript">
  tinyMCE.init({
    mode:  "exact",
    elements:                          "%s",
    theme:      					   "%s",
    paste_data_images: true,
    plugins:						   "autolink,code,lists,spellchecker,layer,table,save,insertdatetime,preview,media,searchreplace,contextmenu,paste,directionality,fullscreen,noneditable,link,visualchars,nonbreaking, image,emoticons,hr",
    toolbar:      					   "undo redo | styleselect | bold italic textcolor fontselect fontsizeselect | alignleft aligncenter alignright alignjustify | link unlink | image | spellchecker | emoticons",
    %s
    %s
    theme_advanced_toolbar_location:   "top",
    theme_advanced_toolbar_align:      "left",
    theme_advanced_statusbar_location: "none",
    theme_advanced_resizing:           false,
    theme_advanced_blockformats : "p,div,h1,h2,h3",
    language : 							"fr",
        theme_advanced_buttons1 : "undo,redo,fullscreen,justifyleft,justifycenter,justifyright,justifyful",
        theme_advanced_buttons2 : "formatselect,fontselect,fontsizeselect,bold,italic,underline,strikethrough,backcolor,forecolor,link,unlink,code"     
  });
</script>
EOF
    ,
      $this->generateId($name),
      $this->getOption('theme'),
      $this->getOption('width')  ? sprintf('width:                             "%spx",', $this->getOption('width')) : '',
      $this->getOption('height') ? sprintf('height:                            "%spx",', $this->getOption('height')) : '',
      $this->getOption('config') ? ",\n".$this->getOption('config') : ''
    );

    return $textarea.$js;
  }
  /**
   * Gets the JavaScript paths associated with the widget.
   *
   * @return array An array of JavaScript paths
   */
  public function getJavaScripts()
  {
    //TODO: check if jquery is loaded
    /*if (sfConfig::has('sf_jquery_web_dir') && sfConfig::has('sf_jquery_core'))
      $jquery = sfConfig::get('sf_jquery_web_dir').'/js/'.sfConfig::get('sf_jquery_core');
    else
      $jquery = '/sfJQueryUIPlugin/js/jquery-1.3.1.min.js';*/
	 $culture = $this->getOption('culture');
    //$js = array('/sfJQueryUIPlugin/js/jquery-ui.js', '/sfJQueryUIPlugin/js/i18n/ui.datepicker-'.substr($culture,0,2).".js");
    $js = array('/js/tiny_mce/tinymce.min.js');
    return $js;
  }
  /*
   *
   * Gets the stylesheet paths associated with the widget.
   *
   * @return array An array of stylesheet paths
   */
  public function getStylesheets()
  {
    $theme = '/css/widgettiny.css';
    //$this->getOption('theme');
    return array($theme => 'screen');
  }
}
